<template>
  <div class="headcom">
    <div class="head_left">
      <img src="../assets/logo.png" alt="" />
      <span>进思学员后台管理系统</span>
    </div>
    <div class="head_right">
      <el-link icon="el-icon-s-home">东方大学</el-link>
      <el-link icon="el-icon-share">进思教育</el-link>
      <el-link icon="el-icon-lock" @click="safeHome = true">安全中心</el-link>
      <el-link icon="el-icon-switch-button" @click="quit">退出</el-link>
      <el-link>{{ userObj.userName }}</el-link>
      <img src="../assets/logo.png" alt="" />
    </div>
    <!-- 弹出层 -->
    <el-dialog width="40%" title="修改密码" :visible.sync="safeHome">
      <el-form
        ref="passForm"
        :rules="ruleObj"
        :model="formObj"
        label-width="120px"
      >
        <el-form-item label="原密码：" prop="userPwd">
          <el-input v-model="formObj.userPwd"> </el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPwd">
          <el-input v-model="formObj.newPwd"> </el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="surePwd">
          <el-input v-model="formObj.surePwd"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="sureEdit">确认修改</el-button>
        <el-button type="danger" @click="safeHome = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { EditPass, getUser, quitLogin } from '../api/common'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      safeHome: false,
      ruleObj: {
        //表单规则
        userPwd: { required: true, message: '原密码必填', trigger: 'blur' },
        newPwd: { required: true, message: '新密码必填', trigger: 'blur' },
        surePwd: { required: true, message: '确认密码必填', trigger: 'blur' }
      },
      formObj: {
        //v-model的数据
        userPwd: '',
        newPwd: '', //新密码
        surePwd: '' //确认密码
      },
      userObj: {}
    }
  },
  created () {
    this.getMyuser()
  },
  methods: {
    ...mapMutations(['setPower']),
    async quit () {
      //退出登录
      var res = await quitLogin()
      this.$message.success('退出成功')
      this.$router.push('/login')
      localStorage.token = ''
      this.$store.dispatch('ActionToken', '')
      console.log(res)
    },
    async getMyuser () {
      //获取用户信息
      var res = await getUser()
      console.log(res)
      this.userObj = res.data
      //将后台返回的权限编码 存储到vuex
      this.setPower(res.data.power)
    },
    async sureEdit () {
      // 获取表单 调用表单的验证方法
      this.$refs.passForm.validate(async vali => {
        if (vali && this.formObj.newPwd == this.formObj.surePwd) {
          //表单验证通过 修改密码接口
          var res = await EditPass({
            userPwd: this.formObj.userPwd,
            newPwd: this.formObj.newPwd
          })
          if (res.data.success) {
            this.$message.success('修改成功，返回重新登录')
            this.$router.push('/login')
          } else {
            this.$message.error('原密码错误')
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.headcom {
  width: 100%;
  height: 80px;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: space-between;
  padding: 0 40px;
  box-sizing: border-box;
  align-items: center;
  color: white;
  .head_left {
    text-align: center;
    line-height: 80px;
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
    }
  }
  .head_right {
    display: flex;
    align-items: center;
    .el-link {
      color: white;
      font-size: 30px;
      margin-right: 30px;
    }
    img {
      width: 50px;
      height: 50px;
    }
  }
}
</style>
